package com.xiaotu.spider.project.pageprocessor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.util.FileUtil;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.APageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.selector.Html;

/**
 * @类名 ChuangShiPageProcessor
 * @日期 2017年3月31日
 * @作者 高海军
 * @功能 创世中文网数据爬取
 */
@Component("ChuangShiPageProcessor")
public class ChuangShiPageProcessor extends APageProcessor
{
	@SuppressWarnings("unchecked")
	@Override
	public void process(Page page)
	{
		Html html = page.getHtml();
		
		List<String> trList = html
				.xpath("//tbody[@id='rankList']/tr[@class!='topline']").all();
		
		if (!RegexUtils.regexFind("\\?p=[0-9]+", page.getUrl().toString()))
		{
			List<String> linkList = html.xpath("//div[@id='pageHTMl']/a/@href")
					.all();
			PageUtils.addTargetRequests(page, linkList.subList(0, 2),
					page.getRequest().getExtras());
			
			FileUtil.writeFile(this.getFileName(page.getRequest().getExtras()),
					StringUtil.list2CSVLine(
							(List<Object>) page.getRequest().getExtra("header"),
							SepratorUtil.SEP_MARK_EN)
							+ SepratorUtil.LINE_SEPRATOR);
		}
		
		for (String trStr : trList)
		{
			Html trObj = new Html("<table>" + trStr + "</table>");
			List<Object> dataList = new ArrayList<>();
			dataList.add(trObj.xpath("//td[1]/strong/text()").get());
			dataList.add(trObj.xpath("//td[2]/text()").get());
			dataList.add(trObj.xpath("//td[3]/a/text()").get());
			
			dataList.add(trObj.xpath("//td[4]/a/text()").get());
			dataList.add(trObj.xpath("//td[5]/text()").get());
			dataList.add(trObj.xpath("//td[6]/span/text()").get());
			
			FileUtil.writeFile(this.getFileName(page.getRequest().getExtras()),
					StringUtil.list2CSVLine(dataList, SepratorUtil.SEP_MARK_EN)
							+ SepratorUtil.LINE_SEPRATOR);
		}
		
		// if (!"全部".equals(page.getRequest().getExtra("type")))
		// return;
		//
		// String divStr = html
		// .xpath("div[@class='tabsswish']/div[@class='rank_publicTab
		// channelTabList' and @style!='display:none']")
		// .get();
		// if (StringUtils.isNotEmpty(divStr))
		// {
		// Html divObj = new Html(divStr);
		// List<String> linkList = divObj.xpath("//a/@href").all();
		// List<String> textList = divObj.xpath("//a/text()").all();
		//
		// for (int i = 1; i < linkList.size(); i++)
		// {
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.putAll(page.getRequest().getExtras());
		// map.put("type", textList.get(i));
		// PageUtils.addTargetRequest(page, linkList.get(i), map);
		// }
		// }
	}
	
	private String getFileName(Map<String, Object> map)
	{
		if (map.get("thematic") == null)
			throw new SpiderException(
					SpiderExceptionCode.DataInvalideException);
		String end = map.get("type") == null ? ""
				: (SepratorUtil.SEP_UNDERLINE + map.get("type"));
		
		return "D:\\chuangshi\\" + map.get("thematic") + end + ".csv";
	}
	
	/**
	 * 设置要爬取的站点地址
	 */
	@SuppressWarnings({ "deprecation" })
	@Override
	protected void setSiteRequest()
	{
		Map<String, Object> tvMap = new HashMap<String, Object>();
		tvMap.put("tvid", "1");
		tvMap.put("tvName", "1");
		String[] urlList = new String[] {
				"http://chuangshi.qq.com/bang/rq/all-zong.html",
				"http://chuangshi.qq.com/bang/nv/all-zong.html" };
		String[] thematicArr = new String[] { "人气榜", "女生人气榜" };
		Object[] headArr = new Object[] { Arrays.asList(
				new String[] { "排名", "分类", "书名", "作者", "人气值", "最近更新日期" }),
				Arrays.asList(new String[] { "排名", "分类", "书名", "作者", "人气值",
						"最近更新日期" }) };
		for (int i = 0; i < urlList.length; i++)
		{
			String url = urlList[i];
			Request request = new Request(url);
			Map<String, Object> extMap = new HashMap<String, Object>();
			extMap.putAll(tvMap);
			request.setExtras(extMap);
			request.putExtra("thematic", thematicArr[i]);
			request.putExtra("type", "全部");
			request.putExtra("header", headArr[i]);
			this.site.addStartRequest(request);
		}
		
	}
	
	@Override
	protected String getSpiderType()
	{
		return "ChuangShi";
	}
	
	@Override
	public String getPageDataType(Page page)
	{
		return "IP剧";
	}
	
	@Override
	protected Request getStartRequest(JSONObject target)
	{
		return null;
	}
}
